package cn.gxg.test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * Oracle数据库中数据字段转换为数据字典
 * @author ABBE
 *
 */
public class Dic2Dic {
	
	public static void main(String[] args) {
		try {
			String url = "jdbc:oracle:thin:@172.18.130.113:1521:fzdbtest";
			String user = "analy_tcode";
			String password = "analy_tcode-1129";
			Connection conn = null;
			
			
			try {
				conn = (Connection) DriverManager.getConnection(url,user,password);
				// 关闭事务自动提交
				conn.setAutoCommit(false);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Statement stm=conn.createStatement();
			ResultSet rs=stm.executeQuery("select DIC_VALUE from T_DATA_DICTIONARY where DIC_CODE='SUPPORTER_TYPE'");
			
			StringBuffer sql = new StringBuffer();
			sql.append("insert into T_DATA_DIC " +
					"(KEY,VALUE)" +
					" values (?,?)");
			Class.forName("oracle.jdbc.driver.OracleDriver");
			PreparedStatement pst = (PreparedStatement) conn.prepareStatement(sql.toString());
			
			System.out.println("开始插入数据！");
			while(rs.next()) {
				String value = rs.getString(1);
				String[] tempStr1=value.split(";");
				for(String str : tempStr1){
					String[] tempStr2=str.split("=");
					pst.setString(1, "SPID_"+tempStr2[0]);
					pst.setString(2, tempStr2[1]);
					// 把一个SQL命令加入命令列表
					pst.addBatch();
				}
				
				// 执行批量更新
				pst.executeBatch();
				// 语句执行完毕，提交本事务
				conn.commit();
			}
			rs.close();
			stm.close();
			
			pst.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
